package com.meilishuo.android.performance;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;

import java.io.*;

/**
 * Created by Xuemeng Wang on 15/3/18.
 * 解析抓取到的流量dat
 */
public class NetInfoNew {
    private static final String netBaseDir="target/android-info/net";
    private static final String netDatDir="target/android-info/net/dat";
    private static final String txNetFile = "target/android-info/net/dat/tx.dat"; //上行
    private static final String rxNetFile = "target/android-info/net/dat/rx.dat"; //下行
    private static final String htmlDir = "target/android-info/net/html";
    private static final String runInfo="target/android-info/run-info.txt";
    private static final String jpgFile = "target/android-info/mem/html/mem.jpg";
    private static final String url = "http://agile.meiliworks.com/data_mgr/save/dashboard";
    private static int txFirst = 0;
    private static int rxFirst = 0;
    private static int txEnd = 0;
    private static int rxEnd = 0;

    /**
     * 解析抓取到的
     */
    public static void getNetInfoFromDat()
    {

        try {
            BufferedReader txBufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(txNetFile)));
            BufferedReader rxBufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(rxNetFile)));
            String txLine = null;
            String rxLine = null;


            int pos = 0;
            while((txLine=txBufferedReader.readLine())!=null && (rxLine=rxBufferedReader.readLine())!=null)
            {
                if(pos == 0) {
                    txFirst = Integer.parseInt(txLine); //初始上传流量
                    rxFirst = Integer.parseInt(rxLine); //初始下载流量
                }
                else
                {
                    txEnd = Integer.parseInt(txLine);
                    rxEnd = Integer.parseInt(rxLine);
                }
                pos++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        //将结果写入到html中
        File file = new File(htmlDir+File.separator+"net.html");
        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"));
            bufferedWriter.write("<html>\n" +
                    "<head>\n" +
                    "<meta charset=\"UTF-8\" />" +
                    "<title>JSChart</title>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "<h3>上行wlan消耗流量:" + (txEnd-txFirst)/(1024.0*1024) + "MB</h3>\n" +
                    "<h3>下行wlan消耗流量:" + (rxEnd-rxFirst)/(1024.0*1024) + "MB</h3>\n" +
                    "</body></html>");
            bufferedWriter.flush();
            bufferedWriter.close();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 计算出最大值，最小值，平均值，然后推送到服务器
     */
    public void pushInfo2Server()
    {
        String sceneInfo = null;
        String startTime = null;
        String endTime = null;
        //读取运行信息
        File file = new File(runInfo);
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            sceneInfo = bufferedReader.readLine(); //第一行场景信息，monkey，静置等
            startTime = bufferedReader.readLine();  //第二行开始运行时间
            endTime = bufferedReader.readLine();  //第三行结束运行时间

        } catch (Exception e)
        {
            e.printStackTrace();
        }

        HttpClient httpClient = HttpClients.createDefault();
        try {
            StringEntity stringEntity = new StringEntity("{\"env\":\"MOB\", \"type\":\"TRAFFIC\", \"scene\":\""+sceneInfo+"\", \"runStartTime\":\""+startTime+"\", \"runEndTime\":\""+endTime+"\", \"data\":{\"tx\":"+(txEnd-txFirst)+", \"rx\":"+(rxEnd-rxFirst)+"}}", "utf-8");
            System.out.println(EntityUtils.toString(stringEntity));
            HttpPost post = new HttpPost(url);
            post.addHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
            post.setEntity(stringEntity);

            HttpResponse response = httpClient.execute(post);
            System.out.println(EntityUtils.toString(response.getEntity()));
            post.releaseConnection();

        } catch (Exception e)
        {
            e.printStackTrace();
        }


    }

    @Test
    public void generateNetHtml()
    {
        getNetInfoFromDat();
        pushInfo2Server();
    }
}
